#include "cameraMiddleWidget.h"

cameraMiddleWidget::cameraMiddleWidget(QWidget *parent) : MiddleWidgetVirtual(parent)
{
    cameraKindComboBox = new QComboBox();
    openBtn = new QPushButton();
    openBtn->setIcon(QIcon(":/openCamera.png"));
    flushBtn = new QPushButton();
    flushBtn->setIcon(QIcon(":/flush.png"));
    infoBtn = new QPushButton();
    infoBtn->setIcon(QIcon(":/info.png"));
    LanguageInit();

    openBtn->setFixedSize(30, 30);
    openBtn->setIconSize(QSize(30, 30));
    flushBtn->setFixedSize(30, 30);
    flushBtn->setIconSize(QSize(30, 30));
    infoBtn->setFixedSize(30, 30);
    infoBtn->setIconSize(QSize(30, 30));

    HLayout = new QHBoxLayout(this);
    HLayout->addWidget(cameraKindComboBox);
    HLayout->addWidget(openBtn);
    HLayout->addWidget(flushBtn);
    HLayout->addWidget(infoBtn);
    HLayout->setMargin(0);
    this->setFixedSize(QSize(FixedWidth, FixedHeight));

    this->setStyleSheet(QSS::QPushButton);
    this->setStyleSheet(QSS::QComboBox);

    senderFlushCamera();
    connect(cameraKindComboBox, SIGNAL(activated(int)), this, SLOT(senderCameraCheck(int)));
    connect(flushBtn, SIGNAL(clicked()), this, SLOT(senderFlushCamera()));
    connect(openBtn, &QPushButton::clicked, this, &cameraMiddleWidget::openCameraBtn);
    connect(infoBtn, &QPushButton::clicked, this, [=]() {ItemIsTarck = !ItemIsTarck; emit ItemTrack(ItemIsTarck); });
}

int cameraMiddleWidget::RFixedHeight()
{
    return FixedHeight;
}

int cameraMiddleWidget::fixedWidth(ParameterKind kind, int widget)
{
    if(!widget)
    ELOG("中心框架尝试修改错误值0");
    if(kind == ParameterKind::Set && widget != 0)
    {
        this->setFixedWidth(FixedWidth);
        FixedWidth = widget;
    }
    return FixedWidth;
}

bool cameraMiddleWidget::setPaletteBF(const QPalette& palette)
{
    this->setPalette(palette);
    this->setAutoFillBackground(true);
    //遍历子组件然后更新他们的调色板
    foreach (QObject *child, this->children())
    {
        if(QWidget* childWidget = qobject_cast<QWidget*>(child))
        {
            childWidget->setPalette(palette);
            this->setAutoFillBackground(true);
        }
    }
    return true;
}

void cameraMiddleWidget::LanguageInit()
{
    openBtn->setToolTip(tr("打开相机"));
    flushBtn->setToolTip(tr("刷新"));
    infoBtn->setToolTip(tr("追踪"));
}

void cameraMiddleWidget::openCameraBtn()
{
    QString currentCameraText = cameraKindComboBox->currentText();
    //qDebug() << "打开摄像头";
    emit cameraCheck(currentCameraText);
}

void cameraMiddleWidget::senderCameraCheck(int text)
{
    QString text1 = (cameras.begin() + text)->description();
    emit cameraCheck(text1);
}

void cameraMiddleWidget::senderFlushCamera()
{
    //点击刷新按钮
    //获取全部摄像头(保存到对应链表中)
    cameras.clear();
    cameraKindComboBox->clear();
    cameras = QCameraInfo::availableCameras();
    for(auto e : cameras)
    {
        cameraKindComboBox->addItem(e.description());
    }
    emit flushCamera();
}

